package com.tyc.nio;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 类描述
 *
 * @author tyc
 * @version 1.0
 * @date 2023-03-08 11:50:31
 */
public class TestNio {
    public static void main(String[] args) throws Exception {
        nioCopy("f://test.txt","f://test2.txt");

    }

    public static void nioCopy(String source,String destination) throws IOException {
        FileChannel sourceChannel = new FileInputStream(source).getChannel();
        FileChannel destinationChannel = new FileOutputStream(destination).getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        while (true){
            buffer.clear();
            int len = sourceChannel.read(buffer);
            if(-1 == len){
                break;
            }
            buffer.flip();
            destinationChannel.write(buffer);
        }
        sourceChannel.close();
        destinationChannel.close();
    }
}
